package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.SystemImage;
import com.tbm.console.proj.repository.SystemImageRepository;
import com.tbm.console.proj.service.SystemImagesService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class SystemImageServiceImpl implements SystemImagesService {

    @Autowired
    private SystemImageRepository systemImageRepository;

    @Override
    public List<SystemImage> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<SystemImage> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<SystemImage> findAll(PageData pageData) {
//        Sort sort = new Sort(Sort.Direction.ASC, "menuOrder");
        Specification<SystemImage> sp = this.getSystemImageSpecification(pageData);
        List<SystemImage> all = systemImageRepository.findAll(sp);
        return all;
    }

    @Override
    public List<SystemImage> findAll(PageData pageData, Integer start, Integer length) {
        return null;
    }

    @Override
    public long count() {
        return 0;
    }

    @Override
    public long count(SystemImage systemImage) {
        return 0;
    }

    @Override
    public long count(PageData pageData) {
        return 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return false;
    }

    @Override
    public boolean deleteByIds(Iterable<SystemImage> iterable) {
        return false;
    }

    @Override
    public SystemImage save(SystemImage systemImage) {
        return new SystemImage();
    }

    @Override
    public List<SystemImage> saveAll(List<SystemImage> systemImages) {
        return systemImageRepository.saveAll(systemImages);
    }

    @Override
    public SystemImage findById(Integer id) {
        return null;
    }

    @Override
    public List<SystemImage> findAllById(Integer[] ids) {
        return null;
    }


    //-------------------------------------------------------------------------------------------//
    //通过Specification来获取自定义的查询条件
    private Specification<SystemImage> getSystemImageSpecification(PageData pageData){
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null != pageData.get("userId")) {
                    preList.add(criteriaBuilder.equal(root.get("userId"), pageData.get("userId").toString()));
                }
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}
